/*    Copyright 2010 Tobias Marschall
 *
 *    This file is part of MoSDi.
 *
 *    MoSDi is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    MoSDi is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MoSDi.  If not, see <http://www.gnu.org/licenses/>.
 */

package mosdi.tests;

import junit.framework.TestCase;
import mosdi.fa.Alphabet;
import mosdi.index.SuffixTree;
import mosdi.util.Iupac;

public class SuffixTreeTest extends TestCase {
	
	public static final String text = "TAACTAAGATTACCAGCCATGGTGGAGTGGATTAATAGCAAAGAAGTCGAGCCCCGAATAAATAAAGGGAGAAAGGGCTTATCCGCGCTGATGCGCGCGG";
	
	public void testSuffixTree() {
		String s = "mississippi";
		Alphabet alphabet = new Alphabet(s);
		SuffixTree st = new SuffixTree();
//		st.addNodeConstructionListener(new SuffixTree.NodeConstructionListener() {
//			public void nodeConstructed(int[] suffixStartPositions, int lcp, int parentLcp) {
//				// System.out.println("New node: "+Arrays.toString(suffixStartPositions)+", lcp "+lcp);
//				System.out.println("New node: "+"mississippi$".substring(suffixStartPositions[0], suffixStartPositions[0]+lcp)+" ("+"mississippi$".substring(suffixStartPositions[0]+parentLcp, suffixStartPositions[0]+lcp)+")");
//			}
//		});
		st.buildTree(alphabet.buildIndexArray(s,true), alphabet.size());
		assertEquals(2, st.countMatches(alphabet.buildIndexArray("ssi")));
		assertEquals(1, st.countMatches(alphabet.buildIndexArray("mi")));
		assertEquals(0, st.countMatches(alphabet.buildIndexArray("psimi")));
		assertEquals(2, st.countMatches(alphabet.buildIndexArray("p")));
		assertEquals(1, st.countMatches(alphabet.buildIndexArray("ppi")));
		assertEquals(4, st.countMatches(alphabet.buildIndexArray("i")));
	}

	public void testSuffixTree2() {
		Alphabet dnaAlphabet = Alphabet.getDnaAlphabet();
		SuffixTree st = new SuffixTree(dnaAlphabet.buildIndexArray(text,true), dnaAlphabet.size());
		assertEquals(30, st.countMatches(dnaAlphabet.buildIndexArray("G")));
		assertEquals(3, st.countMatches(dnaAlphabet.buildIndexArray("AGA")));
		assertEquals(1, st.countMatches(dnaAlphabet.buildIndexArray(text)));
		assertEquals(4, st.countMatches(Iupac.toGeneralizedString("WCNV")));
		assertEquals(6, st.countMatches(Iupac.toGeneralizedString("KMNNNNSW")));
		assertEquals(text.length()-8+1, st.countMatches(Iupac.toGeneralizedString("NNNNNNNN")));
		SuffixTree st2 = new SuffixTree(dnaAlphabet.buildIndexArray(text,true), dnaAlphabet.size(), 4);
		assertTrue(st.getNodeCount()>st2.getNodeCount());
		assertEquals(30, st2.countMatches(dnaAlphabet.buildIndexArray("G")));
		assertEquals(3, st2.countMatches(dnaAlphabet.buildIndexArray("AGA")));
		assertEquals(1, st2.countMatches(dnaAlphabet.buildIndexArray(text)));
		assertEquals(4, st2.countMatches(Iupac.toGeneralizedString("WCNV")));
		assertEquals(text.length(), st2.countMatches(Iupac.toGeneralizedString("N")));
		assertEquals(text.length()-2+1, st2.countMatches(Iupac.toGeneralizedString("NN")));
		assertEquals(text.length()-3+1, st2.countMatches(Iupac.toGeneralizedString("NNN")));
		assertEquals(text.length()-4+1, st2.countMatches(Iupac.toGeneralizedString("NNNN")));
	}

	public void testSuffixAnnotations() {
		Alphabet dnaAlphabet = Alphabet.getDnaAlphabet();
		SuffixTree st = new SuffixTree(dnaAlphabet.buildIndexArray(text,true), dnaAlphabet.size());
		int[] nodes = st.walk(Iupac.toGeneralizedString("NNNN"));
		int[] maxMatchCountAnnotation = st.calcMaxMatchCountAnnotation(4);
		assertEquals(st.getNodeCount(), maxMatchCountAnnotation.length);
		int[] occurrenceCountAnnotation = st.calcOccurrenceCountAnnotation(); 
		assertEquals(st.getNodeCount(), occurrenceCountAnnotation.length);
		SuffixTree st2 = new SuffixTree(dnaAlphabet.buildIndexArray(text,true), dnaAlphabet.size(),4);
		int[] maxMatchCountAnnotation2 = st2.calcMaxMatchCountAnnotation(4);
		assertEquals(st2.getNodeCount(), maxMatchCountAnnotation2.length);
		int[] occurrenceCountAnnotation2 = st2.calcOccurrenceCountAnnotation();
		assertEquals(st2.getNodeCount(), occurrenceCountAnnotation2.length);
		int[] nodes2 = st2.walk(Iupac.toGeneralizedString("NNNN"));
		assertEquals(nodes.length, nodes2.length);
		for (int i=0; i<nodes.length; ++i) {
			assertEquals(maxMatchCountAnnotation[nodes[i]],maxMatchCountAnnotation2[nodes2[i]]);
			assertEquals(occurrenceCountAnnotation[nodes[i]],occurrenceCountAnnotation2[nodes2[i]]);
		}
	}
	
}
